#include <stdio.h>
#include <string.h>

#include "AC33Mxxx_conf.h"
#include "driver_header.h"
#include "test_header.h"
#include "AC33Mxxx_it.h"

#include "console.h"
#include "slib.h"


char	PD3_I_check = 0;

//#############################################################################
//#
//#
//#
//#############################################################################
void IOINTERRUPT_Test(void)
{
	char 	ch;
	int		service_flag; 
	int		cmd_not_supported; 
	int		disp_menu = 1; 
	int 		i;
	cputs(">>>>> IO-INTERRUPT Test <<<<<\r\n"); 


	//-----------------------------------------------------------------------
	// init variables
	//-----------------------------------------------------------------------
	service_flag = 1; 
	cmd_not_supported = 0; 
	InFlag = InCount = 0; 


	//########################################################
	// test body 
	//########################################################
	while (service_flag)
	{

		//-------------------------------------------------------------------
		// disp menu
		//-------------------------------------------------------------------
		if (disp_menu)
		{
			Disp_IOINTERRUPT_Menu(); 
			disp_menu = 0; 
		}


		
		cputs("\r\nIOINTERRUPT>"); 		


		//-------------------------------------------------------------------
		// clear command buffer
		//-------------------------------------------------------------------
		InData[0] = 0; 


		//-------------------------------------------------------------------
		// get command
		//-------------------------------------------------------------------
		for (;;)
		{
			ch = getstring(); 
			if (ch == ASCII_CARRIAGE_RETURN) break; 



			if(PD3_I_check)
			{

					CSP_GPIO_OutputHigh(GPIO_A,PIN_0);
					CSP_GPIO_OutputLow(GPIO_A,PIN_0);
					for(i=0;i<30000;i++);
					CSP_GPIO_OutputHigh(GPIO_A,PIN_0);
				for(i=0;i<1000000;i++);
			}
				
		}

		cputs("\r\n"); 


		//------------------------------------------------------------------
		//  collection of services 
		//------------------------------------------------------------------
		if (InFlag)
		{
			if (!strncmp(InData, "exit", 4))
			{
				service_flag = 0; 
			}
			else if (!strncmp(InData, "pd3i", 4))
			{
				cmd_not_supported = IOINTERRUPT_TEST_PD3I(InData); 
			}

			else if (!strncmp(InData, "start", 5))
			{
				PD3_I_check = 0;
				cmd_not_supported = IOINTERRUPT_TEST_start(InData); 
			}
			else if (!strncmp(InData, "end", 3))
			{
			//	PD3_I_check =  0;
				cmd_not_supported = IOINTERRUPT_TEST_end(InData); 
			}
			else if (!strncmp(InData, "h", 1))
			{
				disp_menu = 1; 
			}
			else 
			{
				cmd_not_supported = 1; 
			}


			if (cmd_not_supported)
			{
				cmd_not_supported = 0; 
				cputs("This command is not supported...\r\n"); 
			}

		}

		InFlag = InCount = 0; 
	}

	cputs("IOINTERRUPT Test Exit...\r\n\r\n"); 
	InFlag = InCount = 0; 
}



/**
*****************************************************************************************************
* @ Name : Disp_GPIO_Menu
*
*
*
*****************************************************************************************************
*/
void Disp_IOINTERRUPT_Menu (void)
{

	cprintf("	   \r\n");
	cprintf("==[IOINTERRUPT]===========================\r\n"); 
	cprintf("command: start, end , pd3i \r\n");
	cprintf("[h] for displaying menu again\r\n");
	cprintf("===================================\r\n\r\n"); 


}


/**
*****************************************************************************************************
* @ Name : GPIO_TEST_Init 
*
*
*
*****************************************************************************************************
*/
int IOINTERRUPT_TEST_PD3I(char * cmd_string)
{

	NVIC_IntrConfig		nvic_config; 

	UINT32	 	reg_val_D = 0x000000c0;   //  Poart D1 = 0x04 : level trigger , 0x0c : edge trigger /Poart D0 = 1 : level trigger , 3 : edge trigger
	
	//-------------------------------------------------------------------------
	//
	//-------------------------------------------------------------------------
	CSP_PCU_ConfigureFunction(PCU_A, PIN_0, PA0_MUX_PA0); 
	CSP_PCU_Set_Direction_Type(PCU_A, PIN_0, PnCR_OUTPUT_PUSH_PULL);
	CSP_PCU_ConfigurePullup(PCU_A, PIN_0, PnPCR_PULLUP_DISABLE); 


	CSP_PCU_ConfigureFunction(PCU_D, PIN_3, PD3_MUX_PD3); 
	CSP_PCU_Set_Direction_Type(PCU_D, PIN_3, PnCR_INPUT_LOGIC);
	CSP_PCU_ConfigurePullup(PCU_D, PIN_3, PnPCR_PULLUP_ENABLE); 
	CSP_PCU_SET_PnIER(PCU_D, reg_val_D); 
	reg_val_D = 0x00000040;		
	CSP_PCU_SET_PnISR(PCU_D, reg_val_D); 
	CSP_PCU_SET_PnICR(PCU_D, reg_val_D); 
	//------------------------------------------------------------------------------------------
	// interrupt (NVIC)
	//------------------------------------------------------------------------------------------		
	nvic_config.nIRQ_Number = (IRQ_GPIODO); 
	nvic_config.u8Preemption_Priority= PRIO_GPIODO_PREEMPTION; 
	nvic_config.u8Subpriority= PRIO_GPIODO_SUBPRIORITY; 
	nvic_config.u8IntrEnable = INTR_ENABLE; 
	CSP_NVIC_ConfigureInterrupt (NVIC, &nvic_config); 


	cprintf("IOINTERRUPT_TEST_PD3I \r\n"); 

	PD3_I_check = 0x77;

	return (0);
}


/**
*****************************************************************************************************
* @ Name : GPIO_TEST_Init 
*
*
*
*****************************************************************************************************
*/
int IOINTERRUPT_TEST_start(char * cmd_string)
{

	NVIC_IntrConfig		nvic_config; 

	UINT32	 	reg_val_A = 0x00000004;   // Poart A1 = 0x04 : level trigger , 0x0c : edge trigger / Poart A0 = 1 : level trigger , 3 : edge trigger
	UINT32	 	reg_val_B = 0x00000004;   // Poart B1 = 0x04 : level trigger , 0x0c : edge trigger /  Poart B0 = 1 : level trigger , 3 : edge trigger
	UINT32	 	reg_val_D = 0x000000c0;   //  Poart D1 = 0x04 : level trigger , 0x0c : edge trigger /Poart D0 = 1 : level trigger , 3 : edge trigger
	
	//-------------------------------------------------------------------------
	//
	//-------------------------------------------------------------------------
	CSP_PCU_ConfigureFunction(PCU_A, PIN_1, PA0_MUX_PA0); 
	CSP_PCU_ConfigureFunction(PCU_B, PIN_1, PB0_MUX_PB0); 
	CSP_PCU_ConfigureFunction(PCU_D, PIN_3, PD3_MUX_PD3); 
	CSP_PCU_ConfigureFunction(PCU_D, PIN_8, PC0_MUX_PC0); 
	CSP_PCU_ConfigureFunction(PCU_D, PIN_9, PC0_MUX_PC0); 
	CSP_PCU_ConfigureFunction(PCU_D, PIN_10, PC0_MUX_PC0); 


	CSP_PCU_Set_Direction_Type(PCU_A, PIN_1, PnCR_INPUT_LOGIC);
	CSP_PCU_Set_Direction_Type(PCU_B, PIN_1, PnCR_INPUT_LOGIC);
	CSP_PCU_Set_Direction_Type(PCU_D, PIN_3, PnCR_INPUT_LOGIC);
	CSP_PCU_Set_Direction_Type(PCU_D, PIN_8, PnCR_OUTPUT_PUSH_PULL);
	CSP_PCU_Set_Direction_Type(PCU_D, PIN_9, PnCR_OUTPUT_PUSH_PULL);
	CSP_PCU_Set_Direction_Type(PCU_D, PIN_10, PnCR_OUTPUT_PUSH_PULL);


	CSP_PCU_ConfigurePullup(PCU_A, PIN_1, PnPCR_PULLUP_DISABLE); 
	CSP_PCU_ConfigurePullup(PCU_B, PIN_1, PnPCR_PULLUP_DISABLE); 
	CSP_PCU_ConfigurePullup(PCU_D, PIN_3, PnPCR_PULLUP_DISABLE); 
	CSP_PCU_ConfigurePullup(PCU_D, PIN_8, PnPCR_PULLUP_DISABLE); 
	CSP_PCU_ConfigurePullup(PCU_D, PIN_9, PnPCR_PULLUP_DISABLE); 
	CSP_PCU_ConfigurePullup(PCU_D, PIN_10, PnPCR_PULLUP_DISABLE); 


	CSP_PCU_SET_PnIER(PCU_A, reg_val_A); 
	CSP_PCU_SET_PnIER(PCU_B, reg_val_B); 
	CSP_PCU_SET_PnIER(PCU_D, reg_val_D); 



	reg_val_A = 0x00000004;		// Falling edge interrupt mode
	reg_val_B = 0x00000008;		// rising edge interrupt mode
	reg_val_D = 0x000000C0;		// Both of rising and falling edge interrupt mode.

	CSP_PCU_SET_PnISR(PCU_A, reg_val_A); 
	CSP_PCU_SET_PnISR(PCU_B, reg_val_B); 
	CSP_PCU_SET_PnISR(PCU_D, reg_val_D); 
	
	CSP_PCU_SET_PnICR(PCU_A, reg_val_A); 
	CSP_PCU_SET_PnICR(PCU_B, reg_val_B); 
	CSP_PCU_SET_PnICR(PCU_D, reg_val_D); 


	//------------------------------------------------------------------------------------------
	// interrupt (NVIC)
	//------------------------------------------------------------------------------------------		
	nvic_config.nIRQ_Number = (IRQ_GPIOAO); 
	nvic_config.u8Preemption_Priority= PRIO_GPIOAO_PREEMPTION; 
	nvic_config.u8Subpriority= PRIO_GPIOAO_SUBPRIORITY; 
	nvic_config.u8IntrEnable = INTR_ENABLE; 
	CSP_NVIC_ConfigureInterrupt (NVIC, &nvic_config); 

	nvic_config.nIRQ_Number = (IRQ_GPIOBO); 
	nvic_config.u8Preemption_Priority= PRIO_GPIOBO_PREEMPTION; 
	nvic_config.u8Subpriority= PRIO_GPIOBO_SUBPRIORITY; 
	nvic_config.u8IntrEnable = INTR_ENABLE; 
	CSP_NVIC_ConfigureInterrupt (NVIC, &nvic_config); 

	nvic_config.nIRQ_Number = (IRQ_GPIODO); 
	nvic_config.u8Preemption_Priority= PRIO_GPIODO_PREEMPTION; 
	nvic_config.u8Subpriority= PRIO_GPIODO_SUBPRIORITY; 
	nvic_config.u8IntrEnable = INTR_ENABLE; 
	CSP_NVIC_ConfigureInterrupt (NVIC, &nvic_config); 


	cprintf("IOINTERRUPT_TEST_start \r\n"); 

	return (0);
}



/**
*****************************************************************************************************
* @ Name : GPIO_TEST_test 
*
*
*
*****************************************************************************************************
*/
int IOINTERRUPT_TEST_end (char * cmd_string)
{

//	CSP_PCU_SET_PnISR(PCU_A, 0); 
//	CSP_PCU_SET_PnISR(PCU_B, 0); 
//	CSP_PCU_SET_PnISR(PCU_D, 0); 

	CSP_PCU_SET_PnIER(PCU_D, 0); 
//	CSP_PCU_SET_PnISR(PCU_D, 0); 
//	CSP_PCU_SET_PnICR(PCU_D, 0); 
	
	cprintf("IOINTERRUPT_TEST_end \r\n"); 
	
	return (0);
}



